
#第一步
#导包
from re import T
from flask import Flask,request
from flask.helpers import make_response

import redis

#实例化flask
app = Flask(__name__)

#config里导入数据库配置
from config import Config
app.config.from_object(Config)

#配置全局
from flask_cors import CORS
cors = CORS(app)


#第一部创建数据库
from flask_sqlalchemy import SQLAlchemy

#第二部 实例化数据库
db = SQLAlchemy(app)

#第三部构建数据表
class Orders(db.Model):
    __tablename__ = 'orders'
    id  = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name  = db.Column(db.String(64), unique=True,default='')
    price = db.Column(db.Float)

#第四部创建表
# db.create_all()


#第五步添加数据
@app.route('/zuoye/add/',methods=["POST","GET","PUT","DELETE"])
def add():

    #添加
    if request.method =="POST":
        data = request.form

        shop  = Orders(name=data['name'],price=data['price'])

        db.session.add(shop)
        db.session.commit()

        return 'add ok'

    elif request.method =="GET":
        #查看全部
        data = Orders.query.all()
        for i in data:
            print(i.name)

        #查看一个 按照id查询
        # data = Orders.query.get(1)
        # print(data)

        #根据字段查询
        # data = Orders.query.filter_by(name = '花生')
        # print(data)

        return 'show ok'

    elif request.method =="PUT":

        return 'put ok'

    else:

        id = request.args.get('id')

        Orders.query.filter_by(id=id).delete()

        db.session.commit()

        return 'delete ok'

#钩子函数记录
@app.after_request
def after_request(response):
    #第一步链接redis
    conm = redis.Redis()
    #第二部 储存 值
    conm.set('sss','123456789')
    
    return make_response(response)

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=5000)
    






